
#include "gameuilayer.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "levelmanager.h"
#include "resourcemanager.h"
#include "messagequeue.h"

static const int TAG_WAVE_INDICATOR = 100;

GameUILayer::GameUILayer()
{
	holderName = "ui_layer";
}

GameUILayer::~GameUILayer() {

}

bool GameUILayer::init() {
	if ( !Layer::init() ){
		return false;
	}

	MenuItemSprite* btnPauseGame = RM->createButton("textures/ui/pause_game", CC_CALLBACK_1(GameUILayer::onPauseGame, this), this);
	btnPauseGame->setPosition(XY_SCALE(50.0f, GameManager::BASE_HEIGHT - 50));

	_levelTitle = RM->createButtonLabelBMFont(T("level 1"), this);
	_levelTitle->setColor(Color3B(0,0,255));
	_levelTitle->setPosition(XY_SCALE(480, GameManager::BASE_HEIGHT - 50));

	Sprite* sprGoldPad = CREATE_SPRITE("textures/ui/gold_pad.png");
	sprGoldPad->setPosition(XY_SCALE(955, GameManager::BASE_HEIGHT - 50));
	sprGoldPad->setAnchorPoint(Point(1.0f, 0.5f));
	addChild(sprGoldPad);

	_goldQuantity = RM->createButtonLabelBMFont("0", sprGoldPad);
	_goldQuantity->setColor(Color3B(255,0,0));

	scheduleUpdate();

	return true;
}

void GameUILayer::update( float ticks )
{
	handleMessage();

	sprintf(buf, "%d", GDM->money);
	_goldQuantity->setString(buf);
}

void GameUILayer::handleMessage() {
	auto msg = PEEK_MSG(holderName);
	if(msg) {
		switch(msg->key) {
		case MessageType::show_wave_indicator:
			showEnemyWaveIndicator();
			break;
		case MessageType::next_wave_began: {
			_waveIndicators.clear();
			removeNodeByTag(TAG_WAVE_INDICATOR);
			
			break;
		}
		default:break;
		}

		POP_MSG(holderName);
	}
}

bool GameUILayer::ccTouchBegan( Touch *pTouch, Event *pEvent ) {
	bool ret = handleTouchWaveIndicators(pTouch->getLocation());
	return ret;
}

void GameUILayer::ccTouchMoved( Touch *pTouch, Event *pEvent ) {

}

void GameUILayer::ccTouchEnded( Touch *pTouch, Event *pEvent ) {

}

void GameUILayer::ccTouchCancelled( Touch *pTouch, Event *pEvent ) {

}

void GameUILayer::onPauseGame( Object* obj )
{

}

void GameUILayer::showEnemyWaveIndicator()
{
	_waveIndicators.clear();

	vector<int> arr = LM->getWaysOfWave(LM->enemyWaveIndex);

	for(size_t i = 0; i < arr.size(); i++) {
		WayInfo info  = LM->ways[arr[0]];
		Point pointTo = info.waypoints[0];
		Point pos = info.waypoints[1];

		bool shouldSkip = false;
		for(size_t j = 0; j < i; ++j) {
			Point handledPointTo = LM->ways[arr[j]].waypoints[0];
			if(pointTo.getDistance(handledPointTo) < 100) {
				shouldSkip = true;
				break;
			}
		}

		if(!shouldSkip) {
			Sprite* icon = CREATE_SPRITE("textures/ui/round_button_0.png");
			icon->setPosition(SCALE_PT(pos));
			icon->setTag(TAG_WAVE_INDICATOR);
			addChild(icon);

			Sprite* arrow = CREATE_SPRITE("textures/ui/wave_indicator_arrow.png");
			arrow->setScale(1.0f);
			Point arrowPos = GM->getParentCenterPosition(icon);
			arrow->setPosition(arrowPos);
			arrow->setAnchorPoint(Point(pos.x / arrow->getContentSize().width, 0.5f));
			float rotation = (pointTo - pos).getAngle(Point::ZERO) / M_PI * 180.0f;
			log("%0.0f", rotation);
			arrow->setRotation(rotation);
			//arrow->setRotation(180.0f);
			icon->addChild(arrow, -1);

			icon->runAction(
				RepeatForever::create(
				Sequence::create(
				ScaleTo::create(0.5f, 1.2f * GM->minscale), 
				ScaleTo::create(0.5f, 0.8f * GM->minscale), nullptr)));

			_waveIndicators.push_back(icon);
		}
	}
}

void GameUILayer::removeNodeByTag( int tag )
{
	Node* node = getChildByTag(tag);
	while(node) {
		node->removeFromParent();
		node = getChildByTag(tag);
	}
}

void GameUILayer::removeNode( Node* node )
{
	node->removeFromParent();
}

bool GameUILayer::handleTouchWaveIndicators(const Point& pt)
{
	bool touched = false;

	for(Sprite* spr : _waveIndicators) {
		if(spr->getBoundingBox().containsPoint(spr->getParent()->convertToNodeSpace(pt))) {
			touched = true;
			break;
		}
	}

	if(touched) {
		_waveIndicators.clear();

		SEND_MSG_ARGS(holderName, "level_manager", MessageType::call_next_wave, 0);
		removeNodeByTag(TAG_WAVE_INDICATOR);
	}

	return touched;
}














